<?php

namespace Payment\Common\LTFpay\Data\Charge;

use Payment\Common\PayException;
use Payment\Utils\ArrayUtil;

class LiteChargeData extends ChargeBaseData{

	/**
	 * 构建用于支付的签名相关数据
	 *
	 * @return void
	 */
	protected function buildData(){
		// TODO: Implement buildData() method.
		$this->certainSignData = [
			'appId'        => $this->appId,
			'random'       => $this->randomStr,
			'merchantCode' => $this->merchantCode,
			'outTradeNo'   => $this->out_trade_no,
			'totalAmount'  => $this->total_fee,
			'channel'      => $this->payChannel ?? "WXPAY",
			'tradeType'    => $this->tradeType ?? "MINIAPP",
			'notifyUrl'    => $this->notifyUrl,
			'openId'       => $this->open_id,
			'subAppId'     => $this->subAppId,
		];

		$signData = array_merge($this->certainSignData, []);
		if(isset($this->rechargeGiveAmount)){
			$signData['rechargeGiveAmount'] = $this->rechargeGiveAmount;
		}
		if(isset($this->discountAmount)){
			$signData['discountAmount'] = $this->discountAmount;
		}
		if(isset($this->unDiscountAmount)){
			$signData['unDiscountAmount'] = $this->unDiscountAmount;
		}
		if(isset($this->goodsDetail)){
			$signData['goodsDetail'] = $this->goodsDetail;
		}
		if(isset($this->operatorId)){
			$signData['operatorId'] = $this->operatorId;
		}
		if(isset($this->orderRemark)){
			$signData['orderRemark'] = $this->orderRemark;
		}
		if(isset($this->subject)){
			$signData['subject'] = $this->subject;
		}
		if(isset($this->expireMinutes)){
			$signData['expireMinutes'] = $this->expireMinutes;
		}
		if(isset($this->unionId)){
			$signData['unionId'] = $this->unionId;
		}
		if(isset($this->memberId)){
			$signData['memberId'] = $this->memberId;
		}
		if(isset($this->orderType)){
			$signData['orderType'] = $this->orderType;
		}
		// 移除数组中的空值
		$this->retData = ArrayUtil::paraFilter($signData);
	}

	protected function checkDataParam(){
		parent::checkDataParam(); // TODO: Change the autogenerated stub
		if(empty($this->notifyUrl)){
			throw new PayException('支付通知地址不能为空');
		}
		if(empty($this->open_id)){
			throw new PayException('支付的用户Openid不能为空');
		}
		if(empty($this->subAppId)){
			throw new PayException('小程序的APPID不能为空');
		}
	}
}
